﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using KipLink.Models;
using KipLink.App_Code.Util;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Security;
using System.Threading.Tasks;

namespace KipLink.Controllers
{
    public class LinkController : Controller
    {
        string uri="http://localhost:13060/";
        // GET: /Link/
        [Authorize]
        public async Task <ActionResult> Index()
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(uri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await client.GetAsync("api/LinkApi/GetLinkByuserName/"+Membership.GetUser().UserName);
            List<LINK> lists = new List<LINK>();
            if (response.IsSuccessStatusCode)
            {
                lists = await response.Content.ReadAsAsync<List<LINK>>();
            }
            return View(lists);
        }

        // GET: /Link/Details/5
        [Authorize]
        public async Task <ActionResult> Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(uri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.GetAsync("api/LinkApi/" + id);
            List<LINK> lists = new List<LINK>();
            if (response.IsSuccessStatusCode)
            {
                lists = await response.Content.ReadAsAsync<List<LINK>>();
            }
            return View(lists.FirstOrDefault());
        }

        // GET: /Link/Create
        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Link/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public async Task<ActionResult> Create([Bind(Include = "LinkName,Title,Description")] LINK linkmodels)
        {
            if (ModelState.IsValid)
            {
                linkmodels.USERNAME = Membership.GetUser().UserName;  
                //Call API-Post
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(uri);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await client.PostAsJsonAsync("api/LinkApi", linkmodels);
                if (response.IsSuccessStatusCode)
                {
                    Uri gizmoUrl = response.Headers.Location;
                }
                return RedirectToAction("Index");
            }
            return View(linkmodels);
        }

        // GET: /Link/Edit/5
        [Authorize]
        public async Task<ActionResult> Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(uri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.GetAsync("api/LinkApi/" + id);
            List<LINK> lists = new List<LINK>();
            if (response.IsSuccessStatusCode)
            {
                lists = await response.Content.ReadAsAsync<List<LINK>>();
            }
            return View(lists.FirstOrDefault());
        }

        // POST: /Link/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public async Task<ActionResult> Edit([Bind(Include="Id,Username,LinkName,Title,Description")] LINK linkmodels)
        {
            if (ModelState.IsValid)
            {
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(uri);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.PutAsJsonAsync("api/LinkApi/", linkmodels);
                if (response.IsSuccessStatusCode)
                {
                }
            }
            return RedirectToAction("Index");
        }

        // GET: /Link/Delete/5
        [Authorize]
        public async Task<ActionResult> Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(uri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.DeleteAsync("api/LinkApi/"+id);
            if (response.IsSuccessStatusCode)
            {

            }
            return RedirectToAction("Index");
        }
    }
}
